package com.yonyou.utils;


import com.intellij.openapi.project.Project;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.VcsFullCommitDetails;
import git4idea.GitLocalBranch;
import git4idea.GitUtil;
import git4idea.branch.GitBranchUtil;
import git4idea.commands.Git;
import git4idea.commands.GitCommandResult;
import git4idea.repo.GitRepository;
import git4idea.repo.GitRepositoryManager;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @author fengjiening
 * @since 2024-01-26
 */
public class GitUtils {


    public static String getCurrentBranch(@NotNull final Project project) {
        GitRepository repository;
        GitLocalBranch localBranch;
        String branchName = "";
        try {
            repository = GitBranchUtil.getCurrentRepository(project);
            localBranch = repository.getCurrentBranch();
            branchName = localBranch.getName();
        } catch (Exception e) {
            e.getMessage();
        }
        if (branchName == null) {
            branchName = "";
        }
        return branchName;
    }
    public static List<String> getCurrentBranchList(@NotNull final Project project) {
        final GitRepository currentRepository = GitBranchUtil.getCurrentRepository(project);
        if(currentRepository==null){
            List<String> collect = new ArrayList<>();
            return collect;
        }else{
            final Collection<GitLocalBranch> localBranches = GitBranchUtil.getCurrentRepository(project).getBranches().getLocalBranches();
            final List<String> collect = localBranches.stream().map(GitLocalBranch::getName).collect(Collectors.toList());
            return collect;
        }

    }


}
